package com.carcontrol.rotate;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.carcontrol.rotate.base.BaseFragment;


//码表
public class Fragment_Second extends BaseFragment {

    public static String TAG = "2";

    private FrameLayout v;
    private Context context;

    private ImageView iv_needle;    //指针
    private byte[] cmd_needle;

    private long loopTime = 1000;  //时间显示间隔   默认100毫秒
    private boolean isThread = true;
    Thread thread;

    Object[] allParam = new Object[]{
            new Object[]{"iv", new Rect(556*width/width0-width_d, 121*height/height0-height_d, 332*width/width0, 332*height/height0), R.mipmap.timer, false, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
            new Object[]{"iv", new Rect(719*width/width0-width_d, 176*height/height0-height_d, 6*width/width0, 134*height/height0), R.mipmap.needle_second, true, new byte[]{0x00,0x01,0x02,0x03,0x04,0x05}},
    };


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        v = new FrameLayout(getActivity());

        context = v.getContext();

        Log.e("fragment_Second===0", MainActivity.curPos+"==="+isThread);

        initView();

//        isThread = true;
//        loopRead();

        return v;
    }

    protected void doAction1(String myDevice, String myStatus){
//        if(myDevice.equals("d001")){
//            if(myStatus.equals("0001")){  //TODO
//                iv_light.setImageResource(R.mipmap.light_on);
//            }else{
//                iv_light.setImageResource(R.mipmap.light_off);
//            }
//        }
    }

    @Override
    public void onDestroy() {
        Log.e("f2===onDestroy", "===");

        isThread=false;

        if(thread!=null){
            thread.interrupt();
        }

        super.onDestroy();
    }

    @Override
    public void onResume() {
        Log.e("f2===onResume", "===");
        super.onResume();
    }

    @Override
    public void onPause() {
        Log.e("f2===onPause", "===");
        super.onPause();
    }

    @Override
    public void onHiddenChanged(boolean hidden) {

        Log.e("f2===onHiddenChanged", hidden+"===");

        if(hidden){

        }

        super.onHiddenChanged(hidden);
    }

    private void initView(){
        for (int i=0; i<allParam.length; i++){
            Object[] obj = (Object[])allParam[i];
            Rect rect = (Rect)obj[1];
            String type = (String)obj[0];

            if("tv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                String name = (String)obj[2];
                int textSize = (int)obj[3];
                int textColor = (int)obj[4];
                TextView tv = new TextView(context);
                tv.setText(name);
                tv.setTextSize(textSize);
                tv.setTextColor(textColor);
                v.addView(tv, params);
            }else if("iv".equals(type)){
                FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(rect.right, rect.bottom);
                params.setMargins(rect.left, rect.top, rect.right, rect.bottom);

                int imgSrc = (int)obj[2];
                ImageView iv = new ImageView(context);
                iv.setImageResource(imgSrc);
                iv.setScaleType(ImageView.ScaleType.FIT_XY);
                v.addView(iv, params);

                if(imgSrc==R.mipmap.needle_second){
                    int degree=0;

                    Log.e("iv===", iv.getWidth()+"==="+iv.getHeight());

                    iv_needle = iv;
                    cmd_needle = (byte[])obj[4];
                    iv.setPivotX(rect.right/2);
                    iv.setPivotY(rect.bottom-22);//支点在图片中心
                    iv.setRotation(-120+degree);

                }


                boolean isCanClick = (boolean)obj[3];

                if(isCanClick){
                    iv.setOnTouchListener(new View.OnTouchListener() {
                        @Override
                        public boolean onTouch(View v, MotionEvent event) {
                            Log.e("onTouch===", v+"==="+event.getAction());

//                            write(bytes); //TODO

                            return false;
                        }
                    });

                    iv.setOnClickListener(new View.OnClickListener() {
                        float degree;

                        @Override
                        public void onClick(View v) {

                            int imgId = (int)obj[2];
                            byte[] bytes = (byte[])obj[4];

                            Log.e("fragment_Second===iv", imgId+"==="+bytes.length+"==="+iv.getWidth()+"==="+iv.getHeight());

                            if(imgId==R.mipmap.needle_second){
                                degree-=30;

                                iv.setPivotX(iv.getWidth()/2);
                                iv.setPivotY(iv.getHeight()-22);//TODO 支点在图片中心
                                iv.setRotation(degree);     //指针旋转

                            }

//                            write(bytes); //TODO

                        }
                    });
                }

            }

        }
    }


    private void loopRead(){
        thread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (isThread){
                    try {
                        Thread.sleep(loopTime);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                    if(MainActivity.curPos==1){
                        m_myHandler.sendEmptyMessage(1);
                    }


                }

            }
        });
        thread.start();
    }

    protected Handler m_myHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message mes) {
            switch (mes.what) {
                case 0:
                    break;

                case 1:
//                    read(cmd_needle); //TODO 串口获取车速
                    int degree=30;

                    Log.e("f2===", iv_needle.getWidth()+"==="+iv_needle.getHeight());

//                    iv_needle.setPivotX(rect.right/2);
//                    iv_needle.setPivotY(rect.bottom-22);//支点在图片中心
                    iv_needle.setPivotX(iv_needle.getWidth()/2);
                    iv_needle.setPivotY(iv_needle.getHeight()-22);//支点在图片中心  22:中心偏移量
                    iv_needle.setRotation(-120+degree); //-120:起始角度

                    break;


                default:
                    break;
            }
            return false;
        }
    });

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
    }

}

